﻿//创建时间: 2011-07-19
//创建作者: 李 城
//功能说明: 获取最新的版本信息
//最后修改: 2011-07-19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Newtonsoft.Json;
using TRPOP.Common;
using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Data;
using TRPOP.Config;
// 获取某结点下的信息，返回json格式的数据
/*
 * 
 *   如果传递 jsoncallback ，则说明是jquery的$.gejson调用的，那么此时：
 *       访问此页面的数据js是可以跨域访问的(js编程会有跨域的限制)
 *   如果没有传递，则正常的输出JSON数据,此时如果通过js访问数据，是有跨域限制的
 * 
 */
/*
 * 
 *   必须传递的数据:
 *         nodeid  所属结点id
 *         columns 查询的列
 *   可传递的参数:
 *         orderby
 *         where
 *         topnum   ||  pagesize indexpage (从0开始)
 *   返回的数据有两种：
 *        1 分页
 *        2 top n
 *        3 信息错误
 * 
 * 
 * 
 */
namespace TRPOP.CMS.Web.ExtraBus
{
    /// <summary>
    /// 获取最新的版本信息
    /// </summary>
    public class JSON_GetBaseInfo : IHttpHandler
    {
        public static int intMaxPerNum = 50;
        TRPOP.ComSqlDAL.ICommonDAL oDal = TRPOP.CMS.Plugin.ICommonDALAccess.GetInstance();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (RequestHelper.RequestString("jsoncallback").IsEmpty() == false)
            {
                context.Response.Write(RequestHelper.RequestString("jsoncallback") + "(" + GetJSON(context) + ")");
            }
            else
            {
                context.Response.Write(GetJSON(context));
            }
        }
        public string GetJSON(HttpContext context)
        {
            string strNodeIDs = RequestHelper.RequestString("nodeid").SQLFilter();
            string strColumns = RequestHelper.RequestString("columns").SQLFilter();
            JavaScriptObject oJSOBJ = new JavaScriptObject();
            if (string.IsNullOrEmpty(strNodeIDs) || string.IsNullOrEmpty(strColumns))
            {
                oJSOBJ.Add("Status", "error");
                oJSOBJ.Add("ErrorInfo", "传递的参数不正确，没有传递nodeid或columns");
                return JavaScriptConvert.SerializeObject(oJSOBJ);
            }
            string strOrderBy = RequestHelper.RequestString("orderby").SQLFilter();
            string strWhere = RequestHelper.RequestString("where").SQLFilter();
            //查询前n条数据
            int intTopNum = JsUtil.JsonDataLimit(RequestHelper.RequestInt("topnum"), intMaxPerNum);
            //分页信息
            int intPageSize = RequestHelper.RequestInt("pagesize");
            int intIndexPage = RequestHelper.RequestInt("indexpage");
            if (intPageSize == 0)//如果分页信息有误，则查询前200条数据
            {
                intTopNum = intMaxPerNum;
            }
            //如果查询的是前n条数据
            DataSet oDataSet = null;
            int intCountNum = 0;
            try
            {
                strWhere += " AND NodeID in (" + strNodeIDs + ") ";

                if (intTopNum > 0)
                {
                    oDataSet = oDal.GetListByPager(SystemInfoHelper.getSystemInfo().DBprefixTableName + "_vwAllBaseInfo", strColumns, 1, intTopNum, strWhere, strOrderBy, out intCountNum, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                }
                else
                {
                    oDataSet = oDal.GetListByPager(SystemInfoHelper.getSystemInfo().DBprefixTableName + "_vwAllBaseInfo", strColumns, intIndexPage, intPageSize, strWhere, strOrderBy, out intCountNum, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                }
            }
            catch (Exception oEx)
            {
                oJSOBJ.Add("Status", "error");
                oJSOBJ.Add("ErrorInfo", "获取信息时失败: " + oEx.Message);
                return JavaScriptConvert.SerializeObject(oJSOBJ);
            }
            if (oDataSet == null || oDataSet.Tables.Count <= 0 || oDataSet.Tables[0].Rows.Count <= 0)
            {
                oJSOBJ.Add("Status", "error");
                oJSOBJ.Add("ErrorInfo", "没有任何");
                return JavaScriptConvert.SerializeObject(oJSOBJ);
            }
            else
            {
                oJSOBJ.Add("DataCount", intCountNum);
                oJSOBJ.Add("InfoList", JsUtil.GetJsObjByDataTable(oDataSet.Tables[0]));
                return JavaScriptConvert.SerializeObject(oJSOBJ);
            }
        }
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}